import emojis from '../config/emoji.js'
function fillZero(number) {
    let time = number+''
    return time.length ==1?'0'+time:time
}
export function getTime() {
    var now = new Date() 
    var year=now.getFullYear(); 
    var month=now.getMonth()+1; 
    var date=now.getDate(); 
    var hour=fillZero(now.getHours())
    var minute=fillZero(now.getMinutes())
    var second=fillZero(now.getSeconds())
    return year+"-"+month+"-"+date+" "+hour+":"+minute+":"+second; 
}

export function linkToATag(str="") {

    //str=""的默认值写法，并不能防止str == null的情况
    str = str || ""
    let reg = new RegExp('((ht|f)tps?:)//[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]','g') 
    let newStr = str.replace(reg,'<a href="$&" _target="blank">$&</a>')
    return newStr
}

//把html字符串里的表情（[微笑]）标识符，转换成img 
export function resolveEmoji(str) {
    // console.log(str);
    
    let reg = /\[[\u4e00-\u9fa5]+?\]/g
    let newStr = str.replace(reg,(sign)=>{
        let emoji = emojis.find(emoji=>{
            return emoji.ChineseCon == sign
        })
        // console.log(sign);
        // console.log(emoji);
        let url = emoji.ExpressionUrl
        return `<img style="vertical-align: middle;" src="./img/${url}.gif" />`
    })
    return newStr
}